 <?php
include("config.php");
if (!isset($_SESSION["user"]) || $_SESSION["user"] == "") header('Location: index.php');
include("header.php");
include("menu.php");

$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
if ($db_www->isConnected()) {	
	$i_query = str_replace("<kodas>", $_SESSION['user'], $i_query);
	$minfo = $db_www->returnInfo($i_query);	
	if (!empty($minfo)) {
		$skola_query = str_replace("<im_kodas>", $minfo["N08_kodas_ks"], $skola_query);
		$sinfo = $db_www->returnInfo($skola_query);	
        if (!empty($sinfo)) {
			$dskola=$sinfo["skola"];
		} else {
			$dskola=0;
		}
		
		$ns_query2 = str_replace("<im_kodas>", $minfo["N08_kodas_ks"], $ns_query2);
		$ns = $db_www->returnList($ns_query2);			
		

	} else {
		die("Nerasta imone");
	}
	
} else {
	die("Klientų aptarnavimo sistema laikinai neveikia.");
}



?>
<table class="innercont" cellspacing="0" cellpadding="5">
<tr><td valign="top">
<table cellspacing="0" cellpadding="0" border="0"><tr><td>
<?php

if (!empty($ns)) {
?>
</td></tr><tr><td>
<br><br>
<?php
	$td="<td>&nbsp;</td>";
	
	printActiveDocumentsTable($minfo["N08_kodas_ks"]);
?>



</td></tr><tr><td>
<?php
} 
if (!isset($oldstyle) || $oldstyle != "1") {
	printReceivedPaymentsTable($minfo["N08_kodas_ks"]);	
}
?>
</td></tr></table>
</td><td  width="340" valign="top">
<br><br>
<table class="payments" cellspacing="0" cellpadding="5">
<tr>
<td class="paymentsth borderbottom">Kredito limitas:</td><td class="borderbottom borderlr">&nbsp;</td><td class="borderbottom center"><?php echo $minfo["n08_suma_wk_limit"];?></td>
</tr>
<?php
if ($dskola>= 0) {
	if ($dskola == 0)
		$tskola = "";
	else
		$tskola = $dskola;
?>
<tr>
<td class="paymentsth borderbottom">Skola:</td><td class="borderbottom borderlr" style="color:red;"><?php echo $tskola;?><td class="borderbottom center" >&nbsp;</td></td>
</tr>
<?php
} else {
?>
<tr>
<td class="paymentsth borderbottom">Likutis:</td><td class="borderbottom borderlr" >&nbsp;</td><td class="borderbottom center"><?php echo abs($dskola);?></td>
</tr>
<?php
}
if ($kreditas == "1") {

		$lastaccdate_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $lastaccdate_query);
		$lastacc = $db_www->returnInfo($lastaccdate_query);

		if (!isset($lastacc["I31_OP_DATA"])) {
			$pask = "2000.01.01";
		} else {
			$pask = $lastacc["I31_OP_DATA"];
		}
		
		$k_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $k_query);
		$kinfo = $db_www->returnList($k_query);
		$numbers = "'333'";
		foreach ($kinfo as $card) {
			$numbers .= ",'" . trim($card["N51_PAV"]) . "'";
		}

		$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);
		if (!$db_rdata->isConnected()) {	
			die("Klientų aptarnavimo sistema laikinai neveikia(rdata).");
		}
		
		$today = date("Y-m-d");
		
		$imusedcredit_query = str_replace("<kodas>", $numbers, $imusedcredit_query);		
		$imusedcredit_query = str_replace("<from>", str_replace(".","-",$pask), $imusedcredit_query);
		$imusedcredit_query = str_replace("<to>", $today, $imusedcredit_query);
		$info = $db_rdata->returnInfo($imusedcredit_query);		
		//echo $imusedcredit_query;
		
		
	    
?>
<tr>
<td class="paymentsth borderbottom">Einamojo mėnesio pirkimai:</td><td class="borderbottom borderlr" style="color:red;"><?php if($info["Kvitosuma"] == null) echo "0.00"; else echo sprintf("%01.2f",$info["Kvitosuma"]);?><td class="borderbottom center">&nbsp;</td></td>
</tr>
<tr>
<?php 
if($info["Kvitosuma"] == null){
	$nep_cred = $minfo["n08_suma_wk_limit"]-$dskola;
} else {
	$nep_cred = $minfo["n08_suma_wk_limit"]-$dskola -$info["Kvitosuma"];
}
?>
<td class="paymentsth borderbottom"><?php if ($nep_cred >= 0) {echo "Nepanaudotas"; } else { echo "Viršytas";}?> kreditas:</td><td class="borderbottom borderlr" >&nbsp;</td><td class="borderbottom center"><?php echo sprintf("%01.2f",$nep_cred);?></td>
</tr>
<?php
} else {
?>
<tr>
<td class="paymentsth borderbottom">Nepanaudotas kreditas:</td><td class="borderbottom borderlr" >&nbsp;</td><td class="borderbottom center"><?php echo sprintf("%01.2f",$minfo["n08_suma_wk_limit"]-$dskola);?></td>
</tr>
<?php
}

if (!isset($_REQUEST['data'])) {
	$data = lastOfPrevMonth();
} else {
	$data = $_REQUEST['data'];
}
if (!isset($oldstyle) || $oldstyle != "1") {
?>
<tr>
<td class="paymentsth borderbottom" colspan="3" align="right">
<script>
  $(function() {
    $( ".dt" ).datepicker({
      showOn: "button",
      buttonImage: "img/calendar.gif",
      buttonImageOnly: true,
	  dateFormat: "yy.mm.dd",
	  regional: "lt"
    });
  });
  </script>
<form method="post" action="act.php" style="margin:0px;">	
<input class="dt" type='text' name="data" value="<?php echo $data;?>"><br>
<input class="but bgfilter" type = "submit" value = "Suderinimo aktas">
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
	$q = "SELECT * FROM n08_klij_c WHERE N08_c_kodas_ks='".addslashes_mssql($minfo["N08_kodas_ks"])."'";
	$rimone = $db_rdata->returnInfo($q);
	if (trim($minfo["N08_kodas_ls_5"]) != "K00") {
?>
<form method="post">
<table align="right" border="0" width="340">
<tr><td style="width:50%;">Suma nuo kurios perspėti apie kredito limito išnaudojimą</td><td>&nbsp;</td></tr>
<tr><td><input style="width:70px;"  class="inp" type='text' name="crpercent" value="<?php 
if (isset($rimone["N08_c_suma_wk_per"])) {
	if (isset($_REQUEST["crpercent"]) ) {
		echo htmlspecialchars($_REQUEST["crpercent"]);
	} else {
		echo htmlspecialchars($rimone["N08_c_suma_wk_per"]);
	}
}
?>"><input class="but" type="submit" value="Išsaugoti">
</td>
<td><?php
if (isset($_REQUEST["crpercent"])) {
	if (is_numeric($_REQUEST["crpercent"])) {
		$crpercent = intval($_REQUEST["crpercent"]);
		if ($crpercent >= 0) {
			$q = "exec web_upd_klij_percent ".$crpercent.",'".addslashes_mssql($minfo["N08_kodas_ks"])."'";
			$rimone = $db_rdata->execute($q);
			echo "Informacija atnaujinta";
		} else {
			echo "Neteisinga suma";
		}
	
	} else {
		echo "Neteisinga suma";
	}
}
?></td></tr>
</table>
</form>
<?php
}
if (isset($oldstyle) && $oldstyle == "1") {
?>
<br><br>
<form method="post" action="act.php">	
<input type = "submit" value = "Suderinimo aktas">
<input class="dt" type='text' name="data" value="<?php echo $data;?>">
</form>
<?php
}
?>
</td></tr></table>
<?php

include("footer.php");
?>